﻿/// <reference path="bootstrap-select.js" />

var table_counter = 1;
// ======== jquery functions ===============================
$(document).ready(function () {

    //Timeout survey after 49 minutes
    setTimeout(function () { window.location = location.protocol + '//' + location.host + '/acs/logout?session_timedout=1' }, 2940000);

    // New window for instructions and faqs
    $(function () {
        $('a.help, a.new-window').click(function () {
            var w = window.open($(this).attr('href'), "NewWin", "width=400,height=500,resizable=1,scrollbars=1,top=0,right=0,menubar=0,status=0");
            w.focus();
            return false;
        });
    });

    if (!$('#insurance_8_1').attr('checked')) {
        $('#insurance_specify').attr('disabled', 'disabled');
        $('#insurance_specify').css('backgroundColor', '#C0C0C0');
    }

    if (!$('#netsub_7_1').attr('checked')) {
        $('#netsub_specify').attr('disabled', 'disabled');
        $('#netsub_specify').css('backgroundColor', '#C0C0C0');
    }

    if (!$('#compuse_3_1').attr('checked')) {
        $('#compuse_specify').attr('disabled', 'disabled');
        $('#compuse_specify').css('backgroundColor', '#C0C0C0');
    }

    if ($('#dateofbirth_1').length) {
        monthChanged(); // On document ready
        $('#dateofbirth_1').change(function () { monthChanged(); setage('dateofbirth_3', 'dateofbirth_1', 'dateofbirth_2', 'dateofbirth_4'); });
        $('#dateofbirth_2').change(function () { $('.day').css('background-color', 'white'); setage('dateofbirth_3', 'dateofbirth_1', 'dateofbirth_2', 'dateofbirth_4'); });
        $('#dateofbirth_3').change(function () { monthChanged(); setage('dateofbirth_3', 'dateofbirth_1', 'dateofbirth_2', 'dateofbirth_4'); });
    }

    $('.disable_embed').each(function () {
        if (!$(this).attr('checked')) {
            $(this).nextAll('.embed').attr('disabled', 'disabled');
            $(this).nextAll().find('.embed').attr('disabled', 'disabled');

            $(this).nextAll('.embed').css('backgroundColor', '#C0C0C0');
            $(this).nextAll().find('.embed').css('backgroundColor', '#C0C0C0');
        }
    });

    // Focus on first input element on every page, unless login, roster_a or disabled
    if ($('#password').attr('id')) {
        if ($('#un1').val()) {
            $('#password').focus();
        }
        else {
            $('#un1').focus();
        }
    }
    else if ($('#roster_a_1_1').attr('id')) {
        if ($('#roster_a_1_1').val()) {
            $('#roster_a_1_4').focus();
        }
        else {
            $('#roster_a_1_1').focus();
        }
    }
    else if ($('#totalincome_2_1').attr('id')) {
        $('#totalincome_2_1').focus();
    }
    else {
        var input = $(':input').filter(function (index) {
            return !$(this).attr('disabled');
        }).first();
        if (input.attr('type') && input.attr('type').match(/text.*/)) {
            input.focus();
        }
    }

    // Hide name boxes on roster_a,add_1,add_2 pages
    var tables = $('div.ppltbl');
    var numofppl = 0;
    for (i = tables.size() ; i > 0; i--) {
        var table = $('#table_' + i);
        $(table).find('input').each(function (index) {
            var regex = /\S+/;
            if (regex.test($(this).val())) {
                numofppl++;
            }
        })
        if (numofppl == 0) {
            $(table).hide();
        }
        else {
            if (i >= table_counter) {
                table_counter = i + 1;
            }

            if (i == tables.size()) {
                $('#expand').css('display', 'none');
            }
        }
    }

}); // END READY
// =========================================================

function autotab(original, destination, e) {
    if (e === undefined) {
        var code = 48;
    }
    else {
        var code = e.keyCode ? e.keyCode : e.charCode;
    }

    if (code >= 48 && code <= 90 || code >= 96 && code <= 105) {
        if (original.value.length >= original.maxLength) {
            document.getElementById(destination).focus();
        }
    }
}

//function to disable box
function togglebox(box_id, enable) {
    var box = document.getElementById(box_id);

    if (enable) {
        box.disabled = false;
        box.style.backgroundColor = 'white';
        box.focus();
    }
    else {
        box.disabled = true;
        box.style.backgroundColor = '#C0C0C0';
    }

    return true;
}

//function to disable box
function toggleEmbed(check, box_id) {
    var box = document.getElementById(box_id);

    if (check.checked) {
        box.disabled = false;
        box.style.backgroundColor = 'white';
        box.focus();
    }
    else {
        box.disabled = true;
        box.style.backgroundColor = '#C0C0C0';
    }

    return true;
}

function enabledisable(goodi, badi) {
    var good = document.getElementById(goodi);
    var bad = document.getElementById(badi);

    good.disabled = false;
    good.readOnly = false;
    good.style.backgroundColor = 'white';

    bad.disabled = true;
    bad.readOnly = true;
    bad.style.backgroundColor = '#C0C0C0';

    return true;
}

function expandtable(rowmax, tblmax) {
    if (document.getElementById("table_" + table_counter)) {
        var name = "#table_" + table_counter;
        $(name).css('display', 'inline');
        $(name).find(':text').first().focus();
    }
    else {
        alert('You may not enter more than ' + rowmax + ' people.');
    }

    if (table_counter == tblmax) {
        $('#expand').css('display', 'none');
    }
    table_counter++;
}

function monthChanged() {
    var days = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    var month = $('.month').val() - 1,
        year = +$('.year').val();

    // Check for leap year if Feb
    if (month == 1 && year && new Date(year, month, 29).getMonth() != 1) {
        days[1]--;
    }

    // Add/Remove options
    if ($('.day option').length > days[month] + 1) {
        // Remove
        if ($('.day').val() > days[month]) {
            $('.day').css('background-color', '#FDF8DF');
        }
        $('.day option').slice(days[month] + 1).remove();
    } else if ($('.day option').length < days[month] + 1) {
        // Add
        for (var i = $('.day option').length; i <= days[month]; i++) {
            $('<option>').attr('value', i).text(i).appendTo('.day');
        }
    }
}

function setage(year, month, day, outinput) {
    window.focus();

    var today = new Date();
    var nowyear = today.getFullYear();
    var nowmonth = today.getMonth() + 1;
    var nowday = today.getDate();

    var yearv = document.getElementById(year).value;
    var monthv = document.getElementById(month).value;
    var dayv = document.getElementById(day).value;
    var outputobj = document.getElementById(outinput);

    var byear = parseInt(yearv, 10);
    var bmonth = parseInt(monthv, 10);
    var bday = parseInt(dayv, 10);

    var yearlow = nowyear - 126;

    if ((byear >= yearlow && byear <= nowyear) && (bmonth >= 1 && bmonth <= 12)) {
        if (!bday) { bday = 0; }

        if (bmonth == nowmonth && (bday < 1 || bday > 31)) {
            outputobj.value = '';
        }
        else if (byear == nowyear && (bmonth > nowmonth || (bmonth == nowmonth && bday > nowday))) {
            outputobj.value = '';
        }
        else {
            var age = nowyear - byear;

            if (bmonth > nowmonth) { age = age - 1 }  // next birthday not yet reached
            else if (bmonth == nowmonth && nowday < bday) { age = age - 1 }

            if (age <= 0) { age = 0 }
            outputobj.value = age.toString();
        }
    }
    else {
        outputobj.value = '';
    }
}

function oncheckboxselectgoto(inputname, gotolocation) {
    var inputv = document.getElementById(inputname);
    if (inputv.checked == true) {
        document.forms[2].elements[0].value = gotolocation;
    }
}

function setgoto(location) {
    document.forms[2].elements[0].value = location;
}



//function to expand/contract a div
function toggleShow(divid) {
    var div = document.getElementById(divid);

    if (div.style.display == 'none') {
        div.style.display = 'block';
    }
    else {
        if (div.style.display == '') {
            div.style.display = 'block';
        }
        else {
            div.style.display = 'none';
        }
    }
}


//
// Disable the Enter key in IE to prevent buggy form submission
//
function noEnter(evt) {
    var evt = (evt) ? evt : ((event) ? event : null);
    var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);

    if (evt.keyCode == 13) {
        if (!(node.type == "submit" || node.type == "textarea")) {
            return false;
        }
    }
}
document.onkeypress = noEnter;





